
var app = angular.module('myApp', []);

$(function() {
    $.ajaxSetup({cache:false});

    $('input, textarea').focus(function() {
        $('#navFooter').hide();
    });

    $('input, textarea').blur(function() {
        $('#navFooter').show();
    });

    setTimeout(function() {
        $('.ul-normal > li, .ul-float > li, .ol-resume > li, .ul-settings > li, .li-arrow').not('.ul-exp > li').on('touchstart', function() {
            $(this).addClass('li-active');
        });

        $('.ul-normal > li, .ul-float > li, .ol-resume > li, .ul-settings > li, .li-arrow').on('touchend', function() {
            $(this).removeClass('li-active');
        });
    }, 500);
});

var application = function() {
    var navItems = {
        fetcher:
            [{
                title: 'getmission',
                text: '我要接活',
                location: '/fetcher',
                href: urls.urlFetch
            }, {
                title: 'resume',
                text: '简历',
                location: '/resume',
                href: urls.urlResume
            }, {
                title: 'msg',
                text: '消息',
                location: '/msg',
                href: urls.urlMessage
            }, {
                title: 'profile',
                text: '个人',
                location: '/settings',
                href: urls.urlSelf
            }],
        publisher:
            [{
                title: 'publish',
                text: '我要发活',
                location: '/publish',
                href: urls.urlPublish
            }, {
                title: 'manage',
                text: '管理活儿',
                location: '/manage',
                href: urls.urlManage
            }, {
                title: 'msg',
                text: '消息',
                location: '/msg',
                href: urls.urlMessage
            }, {
                title: 'profile',
                text: '个人',
                location: '/settings',
                href: urls.urlSelf
            }]
    };

    //Work duration selection
    var durations = [
        {
            id: 1,
            name: '三天内',
            active: false
        },
        {
            id: 2,
            name: '双周内',
            active: false
        },
        {
            id: 3,
            name: '三个月内',
            active: false
        },
        {
            id: 4,
            name: '长期',
            active: false
        }
    ];

    var validTime = [
        {
            id: 1,
            name: '1天',
            value: '1'
        },
        {
            id: 2,
            name: '3天',
            value: '3'
        },
        {
            id: 3,
            name: '7天',
            value: '7'
        },
        {
            id: 4,
            name: '10天',
            value: '10'
        },
        {
            id: 5,
            name: '长期',
            value: '36600'
        }
    ];

    //Salary selection
    var salary = [
        {
            id: '1',
            name: '自填'
        },
        {
            id: '2',
            name: '面谈'
        },
        {
            id: '3',
            name: '涨经验'
        }
    ];

    //Work location selection
    var locations = [
        {
            id: '1',
            name: '不限'
        },
        {
            id: '2',
            name: '多地'
        },
        {
            id: '3',
            name: '固定城市'
        }
    ];

    //Degree year selection
    var degreeYears = [
        {
            id: 0,
            value: '1990'
        },
        {
            id: 1,
            value: '1991'
        },
        {
            id: 2,
            value: '1992'
        },
        {
            id: 3,
            value: '1993'
        },
        {
            id: 4,
            value: '1994'
        },
        {
            id: 5,
            value: '1995'
        },
        {
            id: 6,
            value: '1996'
        },
        {
            id: 7,
            value: '1997'
        },
        {
            id: 8,
            value: '1998'
        },
        {
            id: 9,
            value: '1999'
        },
        {
            id: 10,
            value: '2000'
        },
        {
            id: 11,
            value: '2001'
        },
        {
            id: 12,
            value: '2002'
        },
        {
            id: 13,
            value: '2003'
        },
        {
            id: 14,
            value: '2004'
        },
        {
            id: 15,
            value: '2005'
        },
        {
            id: 16,
            value: '2006'
        },
        {
            id: 17,
            value: '2007'
        },
        {
            id: 18,
            value: '2008'
        },
        {
            id: 19,
            value: '2009'
        },
        {
            id: 20,
            value: '2010'
        },
        {
            id: 21,
            value: '2011'
        },
        {
            id: 22,
            value: '2012'
        },
        {
            id: 23,
            value: '2013'
        },
        {
            id: 24,
            value: '2014'
        },
        {
            id: 25,
            value: '2015'
        }
    ];

    //Provinces and cities
    var cities = new Object();

    var provinces = new Array('北京', '上海', '天津', '重庆', '河北省', '山西省', '辽宁省', '吉林省', '黑龙江省', '江苏省', '浙江省', '安徽省', '福建省', '江西省', '山东省', '河南省', '湖北省', '湖南省', '广东省', '海南省', '四川省', '贵州省', '云南省', '陕西省', '甘肃省', '青海省', '内蒙古', '广西', '西藏', '宁夏', '新疆', '香港', '澳门', '台湾', '马来西亚');

    cities['']=new Array();

    cities['台湾']=new Array('台北','台南','其他');

    cities['马来西亚']=new Array('Malaysia');

    cities['北京']=new Array();

    cities['上海']=new Array();

    cities['天津']=new Array();

    cities['重庆']=new Array();

    cities['河北省']=new Array('石家庄', '张家口', '承德', '秦皇岛', '唐山', '廊坊', '保定', '沧州', '衡水', '邢台', '邯郸');

    cities['山西省']=new Array('太原', '大同', '朔州', '阳泉', '长治', '晋城', '忻州', '吕梁', '晋中', '临汾', '运城');

    cities['辽宁省']=new Array('沈阳', '朝阳', '阜新', '铁岭', '抚顺', '本溪', '辽阳', '鞍山', '丹东', '大连', '营口', '盘锦', '锦州', '葫芦岛');

    cities['吉林省']=new Array('长春', '白城', '松原', '吉林', '四平', '辽源', '通化', '白山', '延边');

    cities['黑龙江省']=new Array('哈尔滨', '齐齐哈尔', '黑河', '大庆', '伊春', '鹤岗', '佳木斯', '双鸭山', '七台河', '鸡西', '牡丹江', '绥化', '大兴安');

    cities['江苏省']=new Array('南京', '徐州', '连云港', '宿迁', '淮阴', '盐城', '扬州', '泰州', '南通', '镇江', '常州', '无锡', '苏州');

    cities['浙江省']=new Array('杭州', '湖州', '嘉兴', '舟山', '宁波', '绍兴', '金华', '台州', '温州', '丽水');

    cities['安徽省']=new Array('合肥', '宿州', '淮北', '阜阳', '蚌埠', '淮南', '滁州', '马鞍山', '芜湖', '铜陵', '安庆', '黄山', '六安', '巢湖', '池州', '宣城');

    cities['福建省']=new Array('福州', '南平', '三明', '莆田', '泉州', '厦门', '漳州', '龙岩', '宁德');

    cities['江西省']=new Array('南昌', '九江', '景德镇', '鹰潭', '新余', '萍乡', '赣州', '上饶', '抚州', '宜春', '吉安');

    cities['山东省']=new Array('济南', '聊城', '德州', '东营', '淄博', '潍坊', '烟台', '威海', '青岛', '日照', '临沂', '枣庄', '济宁', '泰安', '莱芜', '滨州', '菏泽');

    cities['河南省']=new Array('郑州', '三门峡', '洛阳', '焦作', '新乡', '鹤壁', '安阳', '濮阳', '开封', '商丘', '许昌', '漯河', '平顶山', '南阳', '信阳', '周口', '驻马店');

    cities['湖北省']=new Array('武汉', '十堰', '襄攀', '荆门', '孝感', '黄冈', '鄂州', '黄石', '咸宁', '荆州', '宜昌', '恩施', '襄樊');

    cities['湖南省']=new Array('长沙', '张家界', '常德', '益阳', '岳阳', '株洲', '湘潭', '衡阳', '郴州', '永州', '邵阳', '怀化', '娄底', '湘西');

    cities['广东省']=new Array('广州', '清远', '韶关', '河源', '梅州', '潮州', '汕头', '揭阳', '汕尾', '惠州', '东莞', '深圳', '珠海', '江门', '佛山', '肇庆', '云浮', '阳江', '茂名', '湛江');

    cities['海南省']=new Array('海口', '三亚');

    cities['四川省']=new Array('成都', '广元', '绵阳', '德阳', '南充', '广安', '遂宁', '内江', '乐山', '自贡', '泸州', '宜宾', '攀枝花', '巴中', '达川', '资阳', '眉山', '雅安', '阿坝', '甘孜', '凉山');

    cities['贵州省']=new Array('贵阳', '六盘水', '遵义', '毕节', '铜仁', '安顺', '黔东南', '黔南', '黔西南');

    cities['云南省']=new Array('昆明', '曲靖', '玉溪', '丽江', '昭通', '思茅', '临沧', '保山', '德宏', '怒江', '迪庆', '大理', '楚雄', '红河', '文山', '西双版纳');

    cities['陕西省']=new Array('西安', '延安', '铜川', '渭南', '咸阳', '宝鸡', '汉中', '榆林', '商洛', '安康');

    cities['甘肃省']=new Array('兰州', '嘉峪关', '金昌', '白银', '天水', '酒泉', '张掖', '武威', '庆阳', '平凉', '定西', '陇南', '临夏', '甘南');

    cities['青海省']=new Array('西宁', '海东', '西宁', '海北', '海南', '黄南', '果洛', '玉树', '海西');

    cities['内蒙古']=new Array('呼和浩特', '包头', '乌海', '赤峰', '呼伦贝尔盟', '兴安盟', '哲里木盟', '锡林郭勒盟', '乌兰察布盟', '鄂尔多斯', '巴彦淖尔盟', '阿拉善盟');

    cities['广西']=new Array('南宁', '桂林', '柳州', '梧州', '贵港', '玉林', '钦州', '北海', '防城港', '南宁', '百色', '河池', '柳州', '贺州');

    cities['西藏']=new Array('拉萨', '那曲', '昌都', '林芝', '山南', '日喀则', '阿里');

    cities['宁夏']=new Array('银川', '石嘴山', '吴忠', '固原');

    cities['新疆']=new Array('乌鲁木齐', '克拉玛依', '喀什', '阿克苏', '和田', '吐鲁番', '哈密', '博尔塔拉', '昌吉', '巴音郭楞', '伊犁', '塔城', '阿勒泰');

    cities['香港']=new Array();

    cities['澳门']=new Array();

    function getNavItems() {
        return navItems;
    }

    function getDurations() {
        return durations;
    }

    function getValidTime() {
        return validTime;
    }

    function getSalary() {
        return salary;
    }

    function getLocations() {
        return locations;
    }

    function getDegreeYears() {
        return degreeYears;
    }

    function getProvinces() {
        return provinces;
    }

    function getCities() {
        return cities;
    }

    return {
        getNavItems: getNavItems,
        getDurations: getDurations,
        getValidTime: getValidTime,
        getSalary: getSalary,
        getLocations: getLocations,
        getDegreeYears: getDegreeYears,
        getProvinces: getProvinces,
        getCities: getCities
    }
}();

// Directives listening control
app.directive('scroll', function($window) {
    return function(scope, element, attrs) {
        angular.element($window).bind('scroll', function() {
            if (this.scrollY >= 10 || scope.searchForm.keyword.$dirty) {
                scope.enableSearch = true;
            } else {
                scope.enableSearch = false;
            }
            scope.$apply();
        });
    };
});

app.directive('ngBlur', function() {
    return function(scope, element, attrs) {
        element.bind('blur', function() {
            if (document.body.offsetHeight > document.body.clientHeight)
                scope.$apply(attrs.ngBlur);
        });
    };
});

// Directives on form validation
app.directive('regPhone', function() {
    return {
        require: 'ngModel',
        link: function(scope, element, attrs, ctrl) {
            var regPhone = /^1[34578]{1}\d{9}$/;
            scope.$watch(attrs.ngModel, function() {
                if (regPhone.exec($(element).val()))
                    ctrl.$setValidity('regphone', true);
                else {
                    ctrl.$setValidity('regphone', false);
                }
            });
        }
    }
});

app.directive('regPsw', function() {
    return {
        require: 'ngModel',
        link: function(scope, element, attrs, ctrl) {
            var regPsw = /.{6,}/;
            scope.$watch(attrs.ngModel, function() {
                if (regPsw.exec($(element).val()))
                    ctrl.$setValidity('regpsw', true);
                else {
                    ctrl.$setValidity('regpsw', false);
                }
            });
        }
    }
});

app.directive('pswRepeat', function() {
    return {
        require: 'ngModel',
        link: function(scope, element, attrs, ctrl) {
            scope.$watch(attrs.ngModel, function() {
                if (scope.form.pswRepeat == scope.form.psw
                    && !isEmpty(scope.form.psw))
                    scope.regForm.pswRepeat.$setValidity('repeat', true);
                else {
                    scope.regForm.pswRepeat.$setValidity('repeat', false);
                }
            });
        }
    }
});

app.directive('verifyCode', function($http) {
    return {
        require: 'ngModel',
        link: function(scope, element, attrs, ctrl) {
            var url = urls.urlCheckVerifyCode;
            scope.$watch(attrs.ngModel, function() {
                var data = { verfiCode: $(element).val() };
                $http.post(url, data, postCfg(data))
                    .success(function(data) {
                        if (data.status == 1) {
                            ctrl.$setValidity('correct', true);
                        } else {
                            ctrl.$setValidity('correct', false);
                        }
                    })
                    .error(function(data) {
                        ctrl.$setValidity('correct', false);
                    });

            });
        }
    }
});

app.directive('raterFilter', function ($timeout) {
    return {
        restrict: 'A',
        link: function(scope,element,attr) {
            if (scope.$last === true) {
                $timeout(function() {
                    //根据controller的关系是选择$emit或者$broadcast
                    setTimeout(function() {
                        $('.rank').each(function() {
                            $(this).rater({
                                active: true,
                                style: 'inline-normal',
                                maxvalue: 5
                            });
                        });
                    }, 200);
                });
            }
        }
    };
});

app.directive('msgFilter', function ($timeout) {
    return {
        restrict: 'A',
        link: function(scope,element,attr) {
            if (scope.$last === true) {
                $timeout(function() {
                    $("html,body").animate({scrollTop: $('.container').height()}
                        , 0);
                });
            }
        }
    };
});

app.directive('refresh', function($window, $timeout) {
    return {
        estrict: 'A',
        link:function(scope, element, attrs) {
            angular.element($window).bind('scroll', function() {
                    $timeout(function () {
                        if ($window.scrollY + $window.innerHeight >= document.documentElement.scrollHeight
                        && scope.isReqDone) {
                            scope.search(1);
                            scope.$apply();
                        }
                    }, 0);
            });
        }
    }
});